package com.et.launch.push

import android.content.Intent
import android.util.Log
import com.et.launch.App
import com.et.launch.globalUuid
import com.et.launch.util.httpUploadLogResult
import com.et.logger.ELog
import com.et.logger.TAGS
import com.et.util.FtpCallbackListener
import com.et.util.FtpUtil
import com.et.util.logUpload
import org.json.JSONObject

object MessageCenter {
    fun distribute(json: String) {
        val obj = JSONObject(json)
        obj.getString("type").apply {
            when (this) {
                "02" -> {
                    // advert resource
                    Intent().also { intent ->
                        intent.action = "com.et.action.advert.change"
                        intent.putExtra("json", json)
                        ELog.d(TAGS.launch, "launch:send advert change broadcast")
                        App.application.sendBroadcast(intent)
                    }
                }
                "03" -> {
                    // pay way
                    Intent().also { intent ->
                        intent.action = "com.et.action.pay.change"
                        intent.putExtra("json", json)
                        ELog.d(TAGS.launch, "launch:send pay way broadcast")
                        App.application.sendBroadcast(intent)
                    }
                }
                "04" -> {
                    // goods add
                    Intent().also { intent ->
                        intent.action = "com.et.action.goods.add.count"
                        intent.putExtra("json", json)
                        ELog.d(TAGS.launch, "launch:send goods add broadcast")
                        App.application.sendBroadcast(intent)
                    }
                }
                "05" -> {
                    // goods update
                    Intent().also { intent ->
                        intent.action = "com.et.action.goods.change"
                        intent.putExtra("json", json)
                        ELog.d(TAGS.launch, "launch:send goods change broadcast")
                        App.application.sendBroadcast(intent)
                    }
                }
                "09" -> {
                    // discount
                    Intent().also { intent ->
                        intent.action = "com.et.action.goods.discount"
                        intent.putExtra("json", json)
                        ELog.d(TAGS.launch, "launch:send goods discount broadcast")
                        App.application.sendBroadcast(intent)
                    }
                }
                // for machine
                "06" -> {
                    //smq machine config
                    Intent().also { intent ->
                        intent.action = "com.et.action.machine.config"
                        intent.putExtra("json", json)
                        ELog.d(TAGS.launch, "launch:send machine config broadcast")
                        App.application.sendBroadcast(intent)
                    }
                }
                "01" -> {
                    // smq out goods
                    Intent().also { intent ->
                        intent.action = "com.et.action.goods.out"
                        intent.putExtra("json", json)
                        ELog.d(TAGS.launch, "launch:send goods out broadcast")
                        App.application.sendBroadcast(intent)
                    }
                }
                "10" -> {
                    //smq open gate
                    Intent().also { intent ->
                        intent.action = "com.et.action.gate.open"
                        intent.putExtra("json", json)
                        ELog.d(TAGS.launch, "launch:send gate open")
                        App.application.sendBroadcast(intent)
                    }
                }

                "11" -> {
                    // push shell
                }
                // for upgrade
                "08" -> {
                    // upgrade apk
                }
                // for log
                "07" -> {
                    // log push
                    logUpload(globalUuid, obj, object : FtpCallbackListener {
                        override fun onFinish(response: FtpUtil.Response) {
                            Log.d(TAGS.launch, response.success.toString() + response.message)
                            httpUploadLogResult(obj.getString("date"), response)
                        }

                        override fun onError(e: Throwable) {
                            Log.d(TAGS.launch, e.toString())
                        }
                    })
                }
            }
        }
    }
}
